import uploadFactory from '../../utils/uploadFactory'
import InteractionUtil from '../../shell/ui/interaction';
let timer;
Page({
  data: {
    types: [],
    index: 0,
    orderNo: '',
    orderId: '',
    result: [{
      index: 0,
      status: false,
      imgs: [],
      paySno: ''
    }]
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const {
      order,
      orderId
    } = options
    this.setData({
      orderNo: order,
      orderId
    })
    this.getPayVoucher(order)
  },
  onShow(){
    getApp().getSaleMode()
  },
  getPayVoucher(orderno) {
    wx.$http().request({
      url: '/AppMaster/Pay/getPayVoucher',
      data: {
        orderno
      },
      success: res => {
        const {
          data
        } = res.data
        const {
          payment_type,
          voucher_list
        } = data
        const types = payment_type.map(item => {
          return {
            id: item.value,
            label: item.title
          }
        })
        const result = voucher_list.length === 0 ? [{
          index: 0,
          status: false,
          imgs: [],
          paySno: ''
        }] : voucher_list.map(item => {
          const index = types.findIndex(i => i.id * 1 === item.payment_type_id * 1)
          return {
            index,
            status: true,
            imgs: item.voucher_img_list,
            paySno: item.pay_platform_sn,
            pay_voucher_id: item.pay_voucher_id
          }
        })
        this.setData({
          types,
          result
        })
      }
    })
  },
  selectType(e) {
    const {
      dataset
    } = e.currentTarget
    const {
      index
    } = dataset
    const {
      value
    } = e.detail
    let {
      result
    } = this.data
    result[index]['index'] = value * 1
    this.setData({
      result
    })
  },
  uploadImage(e) {
    const {
      dataset
    } = e.currentTarget
    const {
      index
    } = dataset
    InteractionUtil.showLoading(200)
    wx.chooseImage({
      success: (res) => {
        InteractionUtil.hideLoading()
        const {
          tempFilePaths
        } = res
        let {
          result
        } = this.data
        const imgs = result[index]['imgs']
        result[index]['imgs'] = [...imgs, ...tempFilePaths]
        this.setData({
          result
        })
      },
      fail: ()=>{
        InteractionUtil.hideLoading()
        console.log('取消上传')
      }
    })
  },
  addHandler() {
    let {
      result
    } = this.data
    result.push({
      index: 0,
      status: false,
      imgs: []
    })
    this.setData({
      result
    })
  },
  confirmHandler(e) {
    const {
      dataset
    } = e.currentTarget
    const {
      index
    } = dataset
    let {
      result,
      types,
      orderNo
    } = this.data
    const item = result[index]
    const {
      imgs,
      status,
      paySno,
      pay_voucher_id
    } = item
    if (status || imgs.length === 0) { //取消
      if (imgs.length === 0) {
        result.splice(index, 1)
        this.setData({
          result
        })
      } else {
        wx.$http().request({
          url: '/AppMaster/Pay/deletePayVoucher',
          data: {
            pay_voucher_id: pay_voucher_id,
            orderno: orderNo
          },
          success: res => {
            const {
              data
            } = res
            if (data.result === 0) {
              InteractionUtil.showSuccess('删除成功!')
              result.splice(index, 1)
              this.setData({
                result
              })
            }
          }
        })
      }
    } else { //确认提交
      if (imgs.length === 0) {
        InteractionUtil.showNotice('请上传截图!')
        return
      }
      // if (paySno.trim() === '') {
      //   InteractionUtil.showNotice('请填写支付平台流水号!')
      //   return
      // }
      const {
        orderNo
      } = this.data
      const type = types[item.index]
      InteractionUtil.showLoading(200)
      uploadFactory.upLoadFile(item.imgs, 'pay_voucher').then(res => {
        wx.$http().request({
          url: '/AppMaster/Pay/addPayVoucher',
          data: {
            orderno: orderNo,
            payment_type_id: type.id,
            pay_platform_sn: paySno,
            voucher_img_list: res
          },
          success: res => {
            InteractionUtil.hideLoading()
            const {
              data
            } = res
            if (data.result === 0) {
              InteractionUtil.showSuccess('添加成功!')
              result[index]['status'] = true
              result[index]['pay_voucher_id'] = data.data.pay_voucher_id
              this.setData({
                result
              })
            }
          },
          fail: err=>{
            InteractionUtil.hideLoading()
          }
        })
      }).catch(()=>{
        InteractionUtil.hideLoading()
      })
    }
  },
  submitPage(e) {
    const {
      result
    } = this.data
    const flag = result.some(item => !item.status)
    if (flag) {
      InteractionUtil.showNotice('有未提交的截图!')
    } else {
      InteractionUtil.showLoading(300)
      const {
        orderId
      } = this.data
      wx.$http().request({
        url: '/AppMaster/Pay/payOrderWithPayVoucher',
        data: {
          orders_id: orderId
        },
        success: res => {
          InteractionUtil.hideLoading(3000)
          const {
            data
          } = res
          if (data.result === 0) {
            InteractionUtil.showSuccess('支付成功!')
            wx.redirectTo({
              url: '/pages/order/index/index?fromMy=0',
            });
          } else {
            InteractionUtil.hideLoading(3000)
            InteractionUtil.showNotice(data.msg)
          }
        }
      })
    }
  },
  delImg(e) {
    const {
      imgindex,
      index
    } = e.currentTarget.dataset
    let {
      result
    } = this.data
    result[index]['imgs'].splice(imgindex, 1)
    this.setData({
      result
    })
  },
  inputHandler(e) {
    if (timer) clearTimeout(timer)
    timer = setTimeout(() => {
      const {
        index
      } = e.currentTarget.dataset
      const {
        value
      } = e.detail
      let {
        result
      } = this.data
      result[index]['paySno'] = value
      this.setData({
        result
      })
    }, 1000);
  }
})